import { DoctorAuthInfoData, UserInfoData, UserLoginInfoData } from "@/api/entity/user"
import { request } from "@/utils/http/http_request"
import { ApiResponse, Method } from "@/utils/http/http_config"
import { getPlatform } from "@/utils/common/app_util"
import { ConfigData } from "@/api/entity/basic"
import { DepartmentData, DoctorQrData, DoctorVideoData } from "@/api/entity/doctor"
import { DoctorCommitInfoReq, DotorAuthInfoReq } from "@/api/req/user_req"

//获取验证码密码
const getCaptchaPwd = () : Promise<ApiResponse<string>> => {
	return request({
		url: 'doctor/auth/getCaptchaPwd',
		method: Method.GET,
		isShowLoading: true
	})
}

//获取验证码
const getEmsCode = (data : { mobile : string, isLogin : boolean, apiPwd : string, isWeChatLogin ?: boolean }) : Promise<ApiResponse<any>> => {
	return request({
		url: 'doctor/auth/regCaptcha',
		method: Method.POST,
		data: {
			mobile: data.mobile,
			login: data.isLogin,
			wechatLogin: data.isWeChatLogin ?? false,
			apiPwd: data.apiPwd
		},
		isShowLoading: false,
	})
}

//登录或注册
const login = (data : { mobile : string, code : string, isLogin : boolean, deviceType : string, inviteCode ?: string }) : Promise<ApiResponse<UserLoginInfoData>> => {
	return request({
		url: 'doctor/auth/login',
		method: Method.POST,
		data: {
			mobile: data.mobile,
			code: data.code,
			inviteCode: data.inviteCode ?? '',
			deviceType: getPlatform(),
			login: data.isLogin
		},
		isShowLoading: true
	})
}

//获取医生信息
const getUserInfo = () : Promise<ApiResponse<UserInfoData>> => {
	return request({
		url: 'doctor/auth/info',
		method: Method.GET,
		isShowLoading: false
	})
}

//获取医院列表
const getHospitalList = (data : { name : string, page : number, limit : number }) : Promise<ApiResponse<ConfigData[]>> => {
	return request({
		url: 'doctor/dict/hospital',
		method: Method.GET,
		data: data,
		isShowLoading: false
	})
}

//获取科室列表
const getDepartmentList = (organizationId : string = '0') : Promise<ApiResponse<DepartmentData>> => {
	return request({
		url: 'doctor/department/getAllDepartmentList',
		method: Method.GET,
		data: { organizationId: organizationId },
		isShowLoading: true
	})
}

//获取职称列表
const getCategoryList = () : Promise<ApiResponse<ConfigData[]>> => {
	return request({
		url: 'doctor/dict/category',
		method: Method.GET,
		isShowLoading: true
	})
}

//提交个人信息
const saveDoctorProfileInfo = (data : DoctorCommitInfoReq) : Promise<ApiResponse<any>> => {
	return request({
		url: 'doctor/auth/profile',
		method: Method.POST,
		data: data,
		isShowLoading: true
	})
}

//提交认证资料
const saveDoctorAuthInfo = (data : DotorAuthInfoReq) : Promise<ApiResponse<DoctorAuthInfoData>> => {
	return request({
		url: 'doctor/auth/submit',
		method: Method.POST,
		data: data,
		isShowLoading: true
	})
}

//医生退出登录
const userLogout = (showLoading : boolean = true) : Promise<ApiResponse<any>> => {
	return request({
		url: 'doctor/auth/logout',
		method: Method.POST,
		isShowLoading: showLoading
	})
}

//微信登录
const userWechatLogin = (code : string) : Promise<ApiResponse<UserLoginInfoData>> => {
	return request({
		url: 'doctor/auth/wechatLogin',
		method: Method.GET,
		data: { code: code },
		isShowLoading: true
	})
}

//苹果账号登录
const userAppleLogin = (appleCode : string) : Promise<ApiResponse<UserLoginInfoData>> => {
	return request({
		url: 'doctor/auth/appleLogin',
		method: Method.GET,
		data: { appleCode: appleCode },
		isShowLoading: true
	})
}

//微信登录-绑定手机号
const mobileLoginByWeChat = (data : { openId : string, phone : string, code : string }) : Promise<ApiResponse<UserLoginInfoData>> => {
	return request({
		url: 'doctor/auth/wechatLoginByPhone',
		method: Method.GET,
		data: data,
		isShowLoading: true
	})
}

//AppleID登录-绑定手机号
const mobileLoginByApple = (data : { appleCode ?: string, phone : string, code : string }) : Promise<ApiResponse<UserLoginInfoData>> => {
	return request({
		url: 'doctor/auth/appleLoginByPhone',
		method: Method.GET,
		data: data,
		isShowLoading: true
	})
}

//获取医生二维码
const getDoctorQrCode = () : Promise<ApiResponse<DoctorQrData>> => {
	return request({
		url: 'doctor/service/updateDoctorQCode',
		method: Method.PUT,
		isShowLoading: true
	})
}

//获取医生小视频
const getDoctorVideo = () : Promise<ApiResponse<DoctorVideoData>> => {
	return request({
		url: 'doctor/video/getDoctorVideo',
		method: Method.GET,
		isShowLoading: false
	})
}

export {
	getCaptchaPwd,
	getEmsCode,
	login,
	getUserInfo,
	getHospitalList,
	getDepartmentList,
	getCategoryList,
	saveDoctorProfileInfo,
	saveDoctorAuthInfo,
	userLogout,
	userWechatLogin,
	userAppleLogin,
	mobileLoginByWeChat,
	mobileLoginByApple,
	getDoctorQrCode,
	getDoctorVideo,
	
}